<template>
  <div class="container">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>

      <el-form-item label="图片上传">
        <el-upload
          action="http(s)://upload-z1.qiniup.com"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :data="uploadData"
          
        >
      <img v-if="imageUrl" :src="imageUrl" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          <el-button>点击上传</el-button>
        </el-upload>
      </el-form-item>

      <el-form-item label="选择标签" prop="tags">
        <el-checkbox-group v-model="ruleForm.tags">
          <el-checkbox
            :label="i.id"
            :value="i.id"
            name="type"
            v-for="(i, index) in tags"
            :key="index"
          ></el-checkbox>
        </el-checkbox-group>
      </el-form-item>

      <el-form-item label="选择分类" prop="ctype">
        <el-checkbox-group v-model="ruleForm.ctype">
          <el-checkbox
            :label="i.id"
             :value="i.id"
            name="type"
            v-for="(i, inde) in clist"
            :key="inde"
          ></el-checkbox>
        </el-checkbox-group>
      </el-form-item>

      <el-form-item label="选择类别" prop="type">
        <el-radio-group v-model="ruleForm.type">
          <el-radio
            :label="i.id"
             :value="i.id"
            v-for="(i, ss) in types"
            :key="ss"
          ></el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="简介" prop="desc">
        <el-input type="textarea" v-model="ruleForm.desc"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >立即创建</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Class",
  data() {
    return {
      baseurl: "http://rrl8zbud0.hb-bkt.clouddn.com",
      uploadData:{'token':''},
      tags: [],
      types: [],
      clist: [],
      imageUrl: "",
      ruleForm: {
        name: "",
        tags: [],
        ctype: [],
        type: "",
        desc: "",
        url:''
      },
    };
  },

  mounted() {
    // this.getqtoken();
    this.getTage();
    this.getQntoken()
  },

  methods: {
    getQntoken(){
      this.axios.get('/get_qntoken').then(res=>{
        this.uploadData.token = res.data.token
      })
    },
    
    getTage() {
      this.axios.get("/gettags").then((resp) => {
        console.log(resp);
        this.tags = resp.data.tags;
        this.types = resp.data.types;
        this.clist = resp.data.clist;
      });
    },
    submitForm(formName){
      this.$refs[formName].validate((valid)=>{
        if(valid){
          console.log(this.ruleForm)
          // 将图片地址写入
          this.ruleForm.url = this.imageUrl
          console.log(this.ruleForm)
          this.axios.post("/addCourse",this.ruleForm).then(res=>{

          })
          // alert('submit')
        }else{
          console.log('error submit!!');
          return false
        }
      })

      },
      resetForm(formName){
        this.$refs[formName].resetFields();
      },

      handleAvatarSuccess(res) {
      alert(res.key)
      this.imageUrl = "http://rrl8zbud0.hb-bkt.clouddn.com"+res.key
    }


    },

    
}
    
  

</script>

<style scoped ></style>